<template>
  <div class="tree-box">
    <el-tree
      ref="tree"
      :data="treeData"
      node-key="id"
      :props="defaultProps"
      :default-expanded-keys="expandArr"
      :expand-on-click-node="false"
    >
    <span class="custom-tree-node" slot-scope="{ node, data }">
      <span>{{ node.data.code }}</span>
      <div style="width: 80%">
        <span style="text-align: left">{{ node.label }}</span>
      </div>
      <span>
        <el-button type="text" size="mini" @click="() => appendNode(node,data)">添加</el-button>
        <el-button type="text"  size="mini" @click="() => remove(data)">删除</el-button>
      </span>
    </span>
    </el-tree>
    <el-dialog title="新增" :visible.sync="dialogVisible" width="40%">
      <el-form ref="form" :model="addForm" :rules="rules" label-width="50px" style="padding: 0 20px" :validate-on-rule-change="false">
        <el-form-item label="代码" prop="code" style="margin-bottom: 22px">
          <el-input v-model="addForm.code" :placeholder="placeholder" />
        </el-form-item>
        <el-form-item label="名称" prop="value" style="margin-bottom: 22px">
          <el-input v-model="addForm.value" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="handleSubmit('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { countryBusinessTypes, addEconomicType, delEconomicType } from '@/api/modules/systemManage.js'
import { checkCaptialCode, checkTwoNum, checkThreeNum, checkFourNum } from '@/utils/pattern.js'
export default {
  data() {
    return {
      treeData: [],
      defaultProps: {
        label: 'value',
        children: 'child'
      },
      dialogVisible: false,
      addForm: {
        code: '',
        value: '',
        parentId: ''
      },
      checkCaptialCode,
      checkTwoNum,
      rules: {},
      rootLeval: {
        code: [{ required: true, validator: checkCaptialCode, trigger: 'change' }]
      },
      firstLeval: {
        code: [{ required: true, validator: checkTwoNum, trigger: 'change' }]
      },
      secondLeval: {
        code: [{ required: true, validator: checkThreeNum, trigger: 'change' }]
      },
      thirdLeval: {
        code: [{ required: true, validator: checkFourNum, trigger: 'change' }]
      },
      expandArr: ['root'],
      explaneId: '',
      placeholder: ''
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    initData() {
      countryBusinessTypes().then(res => {
        if (res.data.code === '200') {
          this.treeData = res.data.data
          const data = res.data.data
          data.forEach(item => {
            console.log(item);
          })
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    // 点击添加时
    appendNode(node, data) {
      console.log(data)
      console.log(node)
      if (node.level === 5) {
        this.$message.error('最底层不能新增')
      } else {
        this.addForm.parentId = data.id
        this.explaneId = data.id
        this.dialogVisible = true
        if (data.id === 'root') {
          this.rules = this.rootLeval
          this.placeholder = '请输入一位大写英文字母'
        } else if (data.level === '1') {
          this.rules = this.firstLeval
          this.placeholder = '请输入两位数字'
        } else if (data.level === '2') {
          this.rules = this.secondLeval
          this.placeholder = '请输入三位数字'
        } else if (data.level === '3') {
          this.rules = this.thirdLeval
           this.placeholder = '请输入四位数字'
        }
      }
     },
    handleSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addEconomicType(this.addForm).then(res => {
            if (res.data.code === '200') {
              this.$message.success('新增成功')
              this.resetForm('form')
              this.dialogVisible = false
              this.expandArr.push(this.explaneId)
              this.initData()
            } else {
              this.$message.error(res.data.message)
            }
          })
        } else {
          return false
        }
      })
    },
    remove(data) {
      console.log(data)
      this.$confirm('确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const params = {
            id: data.id
          }
          delEconomicType(params).then(res => {
            if (res.data.code === '200') {
              this.$message.success('删除成功')
              this.initData()
            } else {
              this.$message.error(res.data.message)
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })     
        })
      
    },
    handleCancle() {
      this.dialogVisible = false
      this.resetForm('form')
      this.$refs['form'].clearValidate()
      this.rules = {}
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
  .tree-box {
    padding: 30px;
    height: 80vh;
    overflow: auto;
    .custom-tree-node {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 14px;
      padding-right: 8px;
    }
  }
</style>